Skip to content

Bugfix/ipconfig skips protocols#346

Open
AnthonyTTaylor wants to merge 2 commits intouber:masterfrom
AnthonyTTaylor:bugfix/ipconfig-skips-protocols
Open

Bugfix/ipconfig skips protocols#346
AnthonyTTaylor wants to merge 2 commits intouber:masterfrom
AnthonyTTaylor:bugfix/ipconfig-skips-protocols

Conversation

@AnthonyTTaylor
Copy link
Copy Markdown

@AnthonyTTaylor AnthonyTTaylor commented Apr 6, 2026

This PR fixes a regression where protocols (and other mockable entities) inside #if blocks were not being discovered for mock generation. The issue was introduced by changes in PR #328 that made the visitor treat all non-fileMacro #if blocks as conditional import
blocks, even when they contained declarations like protocols.

Problem

For non-fileMacro #if blocks, the visitor now always:

  1. Parses the block as a ConditionalImportBlock
  2. Returns .skipChildren

This is correct for import-only blocks, but incorrect for blocks containing declarations. In those cases, protocols, classes, and other declarations are never visited, so mockable entities are not discovered.

Solution

Only treat an #if block as a conditional import block when it contains imports exclusively.

Added a new recursive method containsOnlyImports in EntityVisitor that checks if an IfConfigDeclSyntax contains only:

  • import declarations
  • nested #if blocks that themselves contain only imports

If the block contains anything else (like protocol, class, etc.), the visitor now returns .visitChildren so entity discovery proceeds normally.

Behavior After This Change

  • Import-only #if blocks continue to be parsed as conditional imports
  • #if blocks containing mockable protocols or other declarations are traversed
  • Mixed blocks fall back to child traversal
  • Nested #if blocks are handled recursively

Only treat #if config blocks as conditional imports when they contain imports exclusively. This preserves existing import parsing while allowing the visitor to traverse blocks that contain @mockable protocols or other declarations.
Copy link
Copy Markdown
Collaborator

@sidepelican sidepelican left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think the containsOnlyImports approach is a bit tricky.
Ideally, I'd like to generate mocks while preserving the structure of the #if directive.

Without #if, isn't it just a coincidence that mock implementations can be used?

@AnthonyTTaylor AnthonyTTaylor force-pushed the bugfix/ipconfig-skips-protocols branch from 900f675 to bd45f5f Compare April 10, 2026 22:18
@AnthonyTTaylor
Copy link
Copy Markdown
Author

AnthonyTTaylor commented Apr 10, 2026

Had to add a missing closing bracket - sorry about the delay.

Hmm, I think the containsOnlyImports approach is a bit tricky.
Ideally, I'd like to generate mocks while preserving the structure of the #if directive.

Without #if, isn't it just a coincidence that mock implementations can be used?

I'll dive in and see about preserving the structure of the #if

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants